//
//  AMapAPIKeyConfigure.swift
//  AMapTest
//
//  Created by haisheng huang on 15/11/6.
//  Copyright © 2015年 haisheng huang. All rights reserved.
//

import UIKit


let APIKey: String = "b35285edcacd6dfae6e3951f465be334"

public class AMapAPIKeyConfigure: NSObject {

    
    public class func configureAPIKey() {
    
        if APIKey.characters.count != 0 {
            
            MAMapServices.sharedServices().apiKey = APIKey
            
        } else {
            
            //提示已经封装到MAMapServices里
            /*let title: String = "提示"
            let message: String = "apiKey为空，请检查key是否正确设置"
            
            if #available(iOS 8.0, *) {
                let alertControllerView = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
                alertControllerView.addAction(UIAlertAction(title: "好", style: UIAlertActionStyle.Cancel, handler: { action in print("....")}))
                AMapAPIKeyConfigure.getCurrentViewController().presentViewController(alertControllerView, animated: true, completion: {})
            } else {
                // Fallback on earlier versions
                let alertView = UIAlertView(title: title, message: message, delegate: self, cancelButtonTitle: "好")
                alertView.show()
            }*/
            
        }
    }
    
    public class func getAPIKey() -> String {
        return APIKey
    }
    
    private class func getCurrentViewController() -> UIViewController {
        
        var curVC: UIViewController
        
        var window: UIWindow = UIApplication.sharedApplication().keyWindow!
        
        if window.windowLevel != UIWindowLevelNormal {
            let windows: NSArray = UIApplication.sharedApplication().windows
            for tmpWin in windows {
                if tmpWin.windowLevel == UIWindowLevelNormal {
                    window = tmpWin as! UIWindow
                    break
                }
            }
        }
        
        let frontView: UIView = window.subviews.first!
        let nextResponder: AnyObject = frontView.nextResponder()!
        if nextResponder is UIViewController {
            curVC = (nextResponder as? UIViewController)!
        } else {
            curVC = window.rootViewController!
        }
        
        return curVC
    }
    
}


